<template>
  <el-card shadow="hover">
    {{emp}}
    <el-form ref="form" :model="emp" label-width="100px">
      <el-form-item label="员工编号">
        <el-input v-model="emp.eid"></el-input>
      </el-form-item>
      <el-form-item label="员工姓名">
        <el-input v-model="emp.ename"></el-input>
      </el-form-item>
      <el-form-item label="出生日期">
        <el-date-picker
          v-model="emp.bdate"
          type="date"
          placeholder="出生日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="员工性别">
        <el-radio v-model="emp.sex" label="M">男</el-radio>
        <el-radio v-model="emp.sex" label="F">女</el-radio>
      </el-form-item>
      <el-form-item label="所在城市">
        <el-input v-model="emp.city"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>

</template>

<script>
  import empApi from '@/api/emp'
  export default {
    data() {
      return {
        emp: {}
      }
    },
    created() {
      this.init()
    },
    methods: {
      init(){
        //1. 得到员工编号
        let eid = this.$route.query.eid
        // 2. 根据员工编号查询员工信息
        if(eid){
          empApi.findById(eid).then(({data}) => {
            this.emp = data.emp
          })
        }
      },
      // 编辑员工
      save() {
        let url = "/emp/add"
        let method = "post"
        if(this.$route.query.eid){
          url = "/emp/update"
          method = "put"
        }
        empApi.save(url,this.emp,method).then(({data}) => {
          this.$router.push("/emp/list")
        })
      }
    }
  }
</script>

<style scoped>
  .el-card{
    width: 800px;
    margin:10px;
  }
</style>

